

jQuery(function ($) {
    var user = JSON.parse(localStorage.getItem('user_info')) || {};
    var id = user.id;
    var login_name = user.user_name;
    $('#yc_name').val(id ? login_name : '畜牧访客');
    var param = {
        "AskCat[]": {
            "AskCat": {
                "use_flag": 1,
                "@order": "rank-"
            }
        }
    };
    send_ajax('post', '/get', param, function callback(data) {
        var html = "";
        if (data.code == 200&&data['AskCat[]']) {
            var data = data['AskCat[]'];
            for (var i = 0; i < data.length; i++) {
                if (!data[i].parent_id) {
                    html += '<option value="" data-id=' + data[i].id + '>' + data[i].name + '</option>';
                }
            }
            $('.cat_list select').append(html);
        }
    });
    $('.btn_lv').click(function () {
        if (!id) {
            alert('你还没有登录，请登录后再操作！');retrun;
        }
        var title = $('#title').val();
        var content = $('#msg').val();
        var cat_id = $('#select option:selected').attr('data-id');
        if (!title) {
            alert('没有填写标题！');
            return;
        } else if (!content) {
            alert('请填写内容！');
            return;
        } else if (!cat_id) {
            alert('请选折问题分类！');
            return;
        }
        var param = {
            "Ask": {
                "title": title,
                "content": content,
                "create_time": getNowFormatDate(),
                "update_time": getNowFormatDate(),
                "user_id": id,
                "cat_id": cat_id
            },
            "Tag": "Ask"
        };
        send_ajax('post', '/post', param, function callback(data) {
            if (data.code == 200) {
                $('#title').val('');
                '#msg'.val('');
                change_pic();
                alert('提交成功');
            }
        });
    });
    //更新图片
    $('.btn_change').click(function () {
        change_pic();
    });
    change_pic();
    function change_pic() {
        var img = document.querySelector('#randCodeImg');
        img.src = host + "/api/code/img?phone=18888888888";
    }
    //获取当前时间
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var strDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
        return currentdate;
    };
});